<?php
namespace app\integral\controller;
use app\AdminRest;
use app\restaurant\model\Order;
use app\shop\model\AdminCoupon;
use app\shop\model\AdminGoods;
use app\shop\model\AdminShopOrder;
use app\shop\model\AdminShopSpePrice;
use app\shop\model\Article;
use app\shop\model\Banner;
use app\shop\model\Date;
use app\shop\model\Goods;
use app\integral\model\Integral;
use app\shop\model\OrderGoods;
use app\shop\model\RefundOrder;
use app\shop\model\Wallet;
use think\App;
use app\shop\model\User as Model;
use think\facade\Db;


class AdminAtv extends AdminRest
{


    protected $model;

    protected $order_goods_model;

    protected $refund_order_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Integral();

    }


    /**
     * @author chenniang
     * @DataTime: 2021-10-29 13:43
     * @功能说明:积分列表
     */
    public function integralList(){

        $this->model->initAtv();

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.status','>',-1];

        $where = [];

        if(!empty($input['goods_name'])){

            $where[] = ['b.name','like','%'.$input['goods_name'].'%'];

            $where[] = ['c.title','like','%'.$input['goods_name'].'%'];
        }

        if(!empty($input['atv_status'])){

            $dis[] = ['a.atv_status','=',$input['atv_status']];
        }

        $data = $this->model->dataGoodsList($dis,$where,$input['limit']);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['goods_price'] = isset($v['goods_info'][0]['goods_price'])?$v['goods_info'][0]['goods_price']:0;

            }

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-10-29 11:58
     * @功能说明:添加积分商城规则
     */
    public function integralAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $res = $this->model->dataAdd($input);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-10-29 13:28
     * @功能说明:积分活动详情
     */
    public function integralInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);

        if($data['goods_type']==1){

            $goods_model = new AdminGoods();

            $data['goods_name'] = $goods_model->where(['id'=> $data['goods_id']])->value('name');

        }else{

            $coupon_model = new AdminCoupon();

            $data['goods_name'] = $coupon_model->where(['id'=> $data['goods_id']])->value('title');

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-10-29 13:30
     * @功能说明:编辑积分规则
     */
    public function integralUpdate(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        if(!empty($input['goods_info'])){


        }

        $res = $this->model->dataUpdate(['id'=>$input['id']],$input);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-11-30 14:56
     * @功能说明:积分兑换记录列表
     */
    public function integralRecordList(){

        $input = $this->_param;

        $dis[] = ['b.integral_id','=',$input['id']];

//        $dis[] = ['a.pay_type','>',1];

        $order_model = new AdminShopOrder();

        $data = $order_model->alias('a')
            ->join('longbing_card_shop_order_item b','a.id = b.order_id')
            ->where($dis)
            ->field('a.*,b.name as goods_name,b.cover as goods_cover,b.content as spe_name,b.price as goods_price,b.integral,b.i_price')
            ->group('b.id')
            ->order('a.id desc')
            ->paginate($input['limit'])
            ->toArray();

        return $this->success($data);


    }














}
